package Customer;

import java.time.LocalDateTime;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        CustomerManager customerManager = new CustomerManager();
        while(true) {
            System.out.println("-----------------------------------------------------------------------------------");
            System.out.println("                             欢迎使用购物管理系统                                      ");
            System.out.println(" 请选择你要执行的操作：                                                                 ");
            System.out.println("                  1.用户端                                                     ");
            System.out.println("                  2.管理员端                                                         ");
            System.out.println("-----------------------------------------------------------------------------------");
            int choose = scanner.nextInt();
            switch (choose) {

                //用户端
                case 1:
                    System.out.println("-----------------------------------------------------------------------------------");
                    System.out.println("                   欢迎使用购物管理系统--用户端                                      ");
                    System.out.println(" 请选择你要执行的操作：                                                                 ");
                    System.out.println("                  1.注册                                                     ");
                    System.out.println("                  2.登录");
                    System.out.println("                  0.登出");
                    System.out.println("-----------------------------------------------------------------------------------");
                    Scanner sc2 = new Scanner(System.in);
                    int choose1 = sc2.nextInt();
                    switch (choose1) {
                        case 1:
                            // 注册
                            Scanner sc4 = new Scanner(System.in);
                            System.out.print("输入用户名: ");
                            String loginUsername = sc4.nextLine();
                            System.out.print("输入密码: ");
                            String loginPassword = sc4.nextLine();
                            System.out.print("输入邮箱：");
                            String loginmail = sc4.nextLine();
                            CustomerManager customerManager1 = new CustomerManager();

                            customerManager1.registerCustomer(loginUsername, loginPassword, loginmail);


                            while (true) {
                                System.out.println("-----------------------------------------------------------------------------------");
                                System.out.println("                   欢迎使用购物管理系统--用户端                                      ");
                                System.out.println(" 请选择你要执行的操作：                                                                 ");
                                System.out.println("                  1.修改密码                                                     ");
                                System.out.println("                  2.商品管理");
                                System.out.println("                  0.登出");
                                System.out.println("-----------------------------------------------------------------------------------");
                                int choose1_1 = sc4.nextInt();
                                switch (choose1_1) {
                                    case 1:

                                        boolean loggedIn = customerManager1.login(loginUsername, loginPassword);
                                        Scanner sc5 = new Scanner(System.in);
                                        if (loggedIn) {
                                            // 修改登录密码
                                            System.out.print("输入新密码: ");
                                            String newPassword = sc5.nextLine();
                                            customerManager1.changePassword(customerManager.customers.get(loginUsername), newPassword);

                                            // 测试忘记密码
                                            System.out.print("输入用于密码重置的用户名: ");
                                            String forgotUsername = sc5.nextLine();
                                            System.out.print("输入用于密码重置的电子邮件: ");
                                            String forgotEmail = sc5.nextLine();
                                            customerManager1.forgotPassword(forgotUsername, forgotEmail);
                                        }
                                        break;

                                    case 2:
                                        //注册一个用户




                                        Customer customer3 = new Customer("customer3", "Customer3@123", "customer3@example.com");

                                        customerManager.registerCustomer("customer3", "Customer3@123", "customer3@example.com");
                                        // 确保customer3已经正确实例化
                                        if (customer3 != null) {
                                            // 测试客户登录
                                            while(true) {
                                                System.out.println("-----------------------------------------------------------------------------------");
                                                System.out.println("                   欢迎使用购物管理系统--用户端-商品管理                                      ");
                                                System.out.println(" 请选择你要执行的操作：                                                                 ");
                                                System.out.println("                  1.添加到购物车                                                     ");
                                                System.out.println("                  2.移出购物车");
                                                System.out.println("                  3.修改购物车");
                                                System.out.println("                  4.结账");
                                                System.out.println("                  5.查看购物历史");
                                                System.out.println("                  0.登出");
                                                System.out.println("-----------------------------------------------------------------------------------");
                                                Scanner sc3 = new Scanner(System.in);
                                                int choose1_2 = sc3.nextInt();
                                                switch(choose1_2) {
                                                    case 1:
                                                        // 测试将商品加入购物车
                                                        Scanner sc7 = new Scanner(System.in);
                                                        System.out.println("添加进购物车:");
                                                        System.out.println("输入商品的ID：");
                                                        String productId = sc7.nextLine();
                                                        System.out.println("输入商品的名称：");
                                                        String productName = sc7.nextLine();
                                                        System.out.println("输入商品的制造商：");
                                                        String manufacturer = sc7.nextLine();
                                                        System.out.println("输入商品的日期：");
                                                        String productionDate = sc7.nextLine();
                                                        System.out.println("输入商品的型号：");
                                                        String model = sc7.nextLine();
                                                        System.out.println("输入商品的买入价格：");
                                                        Double purchasePrice = sc7.nextDouble();
                                                        System.out.println("输入商品的卖出价格：");
                                                        Double retailPrice = sc7.nextDouble();
                                                        System.out.println("输入商品的数量：");
                                                        int quantity = sc7.nextInt();
                                                    Product product = new Product(productId, productName, manufacturer, productionDate, model, purchasePrice, retailPrice, quantity);
                                                    customer3.addToCart(product, quantity);
                                                    break;

                                                    // 测试将商品从购物车中移除
                                                    case 2:
                                                    System.out.println("将商品从购物车中移除:");
                                                    Scanner sc6 = new Scanner(System.in);
                                                    String productId1 = sc6.nextLine();
                                                    customer3.removeFromCart(productId1);
                                                    break;
                                                    // 测试修改购物车中的商品
                                                    case 3:
                                                        Scanner sc6_1 = new Scanner(System.in);
                                                    System.out.println("修改购物车中的商品:");
                                                        System.out.println("输入商品ID：");
                                                    String productId2 = sc6_1.nextLine();
                                                        System.out.println("输入你要修改的个数：");
                                                    int newQuantity = sc6_1.nextInt();
                                                    customer3.modifyCartItem(productId2, newQuantity);
                                                    break;

                                                    // 测试结账
                                                    case 4:
                                                    System.out.println("结账:");
                                                    customer3.checkout("Alipay");
                                                    break;

                                                    // 测试查看购物历史
                                                    case 5:
                                                        System.out.println("购买历史记录:");
                                                        customer3.viewPurchaseHistory();

                                                    break;




                                                }
                                                if(choose1_2 == 0) {
                                                    //登出
                                                    System.out.println("用户登出:");
                                                    customer3.logout();
                                                    break;
                                                }
                                            }
                                        } else {
                                            System.out.println("Customer 实例为 null。初始化失败。");
                                        }
                                        break;

                                }
                                if (choose1_1 == 0) {
                                    System.out.println("登出...");
                                    break;
                                }
                            }
                            break;

                        case 2:
                            System.out.println("请输入用户名：");
                            String username = sc2.nextLine();
                            System.out.println("请输入密码：");
                            String password = sc2.nextLine();

                            System.out.println("未检测到用户");
                            break;

                    }
                    if (choose1 == 0) {
                        // 退出
                        System.out.println("登出...");
                        break;
                    }
                    break;

                    //管理员端
                case 2:
                    while(true){
                        System.out.println("-----------------------------------------------------------------------------------");
                        System.out.println("                   欢迎使用购物管理系统--管理员端                                      ");
                        System.out.println(" 请选择你要执行的操作：                                                                 ");
                        System.out.println("                  1.登录");
                        System.out.println("                  0.登出");
                        System.out.println("-----------------------------------------------------------------------------------");
                        Scanner sc = new Scanner(System.in);
                        int choose2 = sc.nextInt();

                        switch(choose2) {
                            case 1:
                                Admin admin = Admin.getInstance();
                                Scanner sc2_1 = new Scanner(System.in);
                                // 管理员登录
                                System.out.print("输入用户名: ");
                                String username1 = sc2_1.nextLine();
                                System.out.print("输入密码: ");
                                String password1 = sc2_1.nextLine();
                                if (admin.login(username1, password1)) {
                                    System.out.println("成功登入!");
                                    // 修改密码
                                    System.out.print("输入新密码: ");
                                    String newPassword = sc2_1.nextLine();
                                    admin.changePassword(newPassword);


                                } else{
                                    System.out.println("登入失败!");
                                    break;
                                }
                                while (true) {
                                    System.out.println("-----------------------------------------------------------------------------------");
                                    System.out.println("                   欢迎使用购物管理系统--管理员端                                      ");
                                    System.out.println(" 请选择你要执行的操作：                                                                 ");
                                    System.out.println("                  1.用户管理");
                                    System.out.println("                  2.商品管理");
                                    System.out.println("                  0.登出");
                                    System.out.println("-----------------------------------------------------------------------------------");
                                    Scanner sc2_2 = new Scanner(System.in);
                                    int choose2_2 = sc2_2.nextInt();
                                    switch (choose2_2) {

                                        // 重置客户密码
                                        // （假设有顾客存在）
                                        case 1:

                                            Customer customer1 = new Customer(
                                                    "johnDoe",                   // username
                                                    "password123",               // password
                                                    "CUST12345",                 // customerId
                                                    "Standard",                  // userLevel
                                                    LocalDateTime.now(),         // registrationTime
                                                    150.75,                      // totalAmountSpent
                                                    "123-456-7890",              // phoneNumber
                                                    "johndoe@example.com"        // email
                                            );

                                            Customer customer2 = new Customer(
                                                    "janeSmith",                 // username
                                                    "securePass!45",             // password
                                                    "CUST54321",                 // customerId
                                                    "Premium",                   // userLevel
                                                    LocalDateTime.of(2022, 5, 15, 10, 30),  // registrationTime
                                                    5200.00,                     // totalAmountSpent
                                                    "555-234-5678",              // phoneNumber
                                                    "janesmith@premium.com"      // email
                                            );
                                            Customer customer4 = new Customer(
                                                    "newUser01",                 // username
                                                    "tempPassword",              // password
                                                    "CUST98765",                 // customerId
                                                    "Basic",                     // userLevel
                                                    LocalDateTime.now(),         // registrationTime
                                                    0.0,                         // totalAmountSpent
                                                    "444-555-6666",              // phoneNumber
                                                    "newuser01@example.com"      // email
                                            );

                                            admin.addCustomer(customer1);
                                            admin.addCustomer(customer2);
                                            admin.addCustomer(customer4);
                                            admin.listAllCustomers();

                                            while(true) {
                                                System.out.println("-----------------------------------------------------------------------------------");
                                                System.out.println("                   欢迎使用购物管理系统--管理员端                                      ");
                                                System.out.println(" 请选择你要执行的操作：                                                                 ");
                                                System.out.println("                  1.修改密码");
                                                System.out.println("                  2.移除用户");
                                                System.out.println("                  0.返回");
                                                System.out.println("-----------------------------------------------------------------------------------");
                                                Scanner sc2_4 = new Scanner(System.in);
                                                int choose2_3 = sc2_4.nextInt();
                                                admin.listAllCustomers();
                                                switch (choose2_3) {
                                                    case 1:
                                                        Scanner scc1 = new Scanner(System.in);
                                                        System.out.print("输入你想要重置密码的顾客ID: ");
                                                        String customerId = scc1.nextLine();
                                                        System.out.print("输入新密码: ");
                                                        String customerPassword = scc1.nextLine();
                                                        admin.resetCustomerPassword(customerId, customerPassword);
                                                    break;

                                                    case 2:
                                                        Scanner scc2 = new Scanner(System.in);
                                                        System.out.println("输入你要移除用户的Id：");
                                                        String customerId1 = scc2.nextLine();
                                                        admin.deleteCustomer(customerId1);
                                                }
                                                if (choose2_3 == 0){
                                                    break;
                                                }
                                            }


                                            break;

                                        //商品管理
                                        case 2:
                                            while(true){
                                                System.out.println("-----------------------------------------------------------------------------------");
                                                System.out.println("                   欢迎使用购物管理系统--管理员端                                      ");
                                                System.out.println(" 请选择你要执行的操作：                                                                 ");
                                                System.out.println("                  1.添加商品");
                                                System.out.println("                  2.移除商品");
                                                System.out.println("                  3.更新商品");
                                                System.out.println("                  4.查看商品");
                                                System.out.println("                  0.登出");
                                                System.out.println("-----------------------------------------------------------------------------------");
                                                Scanner sc2_3 = new Scanner(System.in);
                                                int choose2_3 = sc2_3.nextInt();
                                                switch(choose2_3){
                                                    case 1:
                                                        Product product = new Product("P000", "Phone", "Samsung", "2024-09-01", "Galaxy", 3000, 3500, 50);
                                                        Product product1 = new Product("P001", "iPad", "Apple", "2024-09-01", "P124", 1000, 1500, 50);
                                                        Product product2 = new Product("P002", "Computer", "Samsung", "2024-09-01", "Galaxy", 2000, 2500, 50);
                                                        admin.addProduct(product);
                                                        System.out.println("\"P000\", \"Phone\", \"Samsung\", \"2024-09-01\", \"Galaxy\", 3000, 3500, 50");
                                                        admin.addProduct(product1);
                                                        System.out.println("\"P001\", \"iPad\", \"Apple\", \"2024-09-01\", \"P124\", 1000, 1500, 50");
                                                        admin.addProduct(product2);
                                                        System.out.println("\"P002\", \"Computer\", \"Samsung\", \"2024-09-01\", \"Galaxy\", 2000, 2500, 50");
                                                        break;
                                                    case 2:
                                                        System.out.println("请输入你要移除产品的编号；");
                                                        Scanner sc2_4 = new Scanner(System.in);
                                                        String productID = sc2_4.nextLine();
                                                        admin.deleteProduct(productID);
                                                    case 3:
                                                        Product product4 = new Product("P004", "Pen", "Samsung", "2024-09-01", "Galaxy", 400, 500, 50);
                                                        admin.addProduct(product4);
                                                        admin.updateProduct("P000",product4);
                                                        break;
                                                    case 4:
                                                        admin.listAllProducts();
                                                        break;
                                                }
                                                if(choose2_3 == 0){
                                                    System.out.println("已返回！");
                                                    break;
                                                }
                                            }


                                            break;

                                    }

                                    if (choose2_2 == 0){
                                        // 登出
                                        admin.logout();
                                        break;
                                    }

                                }
                                break;
                        }
                        if(choose2 == 0){
                            break;

                        }
                        break;
                    }
            }
            if(choose == 0){
                break;

            }
        }
    }
}